package com.bytedance.ttnet.a;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Looper;
import android.os.Message;
import android.util.Pair;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.frameworks.baselib.network.dispatcher.IRequest.Priority;
import com.bytedance.frameworks.baselib.network.http.util.ProcessUtils;
import com.bytedance.ttnet.cc;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

public class NetChannelSelect implements Callback {
    private static final Queue o;
    private static NetChannelSelect l;
    private static boolean p;

    static {
        o = new LinkedBlockingQueue();
        p = true;
    }

    private final Context m;
    private final WeakHandler n;
    public int b;
    public long d;
    public Map e;
    public g$a f;
    int a;
    int c;
    g$a mUiSelectedNetChannel;
    AtomicLong h;
    String i;
    int j;
    AtomicBoolean k;

    private NetChannelSelect(Context arg5) {

        this.n = new WeakHandler(Looper.getMainLooper(), ((Callback) this));
        this.a = 1800;
        this.b = 60;
        this.c = 2;
        this.e = Collections.synchronizedMap(new LinkedHashMap());
        this.h = new AtomicLong(-1);
        this.i = null;
        this.j = -1;
        this.k = new AtomicBoolean(false);
        this.m = arg5;
    }

    public static int a(NetChannelSelect arg1) {
        return arg1.c;
    }

    public static NetChannelSelect a(Context arg2) {
        if (l == null) {
            Class v1 = NetChannelSelect.class;
            synchronized (v1) {
                if (l == null) {
                    l = new NetChannelSelect(arg2);
                }
            }
        }

        return l;
    }

    private static void a(Context arg3, String arg4, JSONObject arg5) {
        if (arg3 != null && !android.support.design.a.isTextEmpty(arg4) && arg5 != null) {
            try {
                if (p) {
                    if (Logger.debug()) {
                        Logger.d("NetChannelSelect", " logType = " + arg4 + " json = " + arg5);
                    }

                    cc.a().a(arg4, arg5);
                    return;
                }

                if (Logger.debug()) {
                    Logger.d("NetChannelSelect", "addToMonitor enqueue");
                }

                o.offer(new Pair(arg4, arg5));
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
    }

    static void a(NetChannelSelect arg5, Context arg6, String arg7, long arg8, String arg10, int arg11, String arg12, int arg13) {
        try {
            JSONObject v0_1 = new JSONObject();
            v0_1.put("id", arg5.h.get());
            v0_1.put("from", arg7);
            v0_1.put("time", arg8);
            v0_1.put("host", arg10);
            v0_1.put("cost", arg11);
            v0_1.put("scheme", arg12);
            v0_1.put("status", arg13);
            a(arg6, "ss_net_channel_select_result", v0_1);
        } catch (Throwable v0) {
        }
    }

    static void a(NetChannelSelect arg0, g$a arg1) {
        arg0.a(arg1);
    }

    static void a(NetChannelSelect arg4, String arg5, Context arg6, boolean arg7, JSONObject arg8, JSONObject arg9) {
        String v1;
        JSONObject v0_1;
        try {
            if (arg9 == null) {
                if ((arg7) && arg4.f != null) {
                    v0_1 = new JSONObject();
                    v0_1.put("id", arg4.h.get());
                    v1 = "pre";
                    v0_1.put(v1, arg8 == null ? "" : arg8);
                    v0_1.put("cur", arg4.f.c());
                    v0_1.put("from", arg5);
                    a(arg6, "ss_net_channel_select", v0_1);
                    return;
                }

                v0_1 = new JSONObject();
                v0_1.put("id", arg4.h.get());
                v1 = "pre";

                v0_1.put(v1, arg8 == null ? "" : arg8);
                v0_1.put("all", arg4.b());
                v0_1.put("from", arg5);
                a(arg6, "ss_net_channel_select", v0_1);
                return;
            }


            arg9.put("from", arg5);
            a(arg6, "ss_net_channel_select", arg9);
            return;
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    static g$a b(NetChannelSelect arg1) {
        return arg1.f;
    }

    static Map c(NetChannelSelect arg1) {
        return arg1.e;
    }

    public static int d(Context arg2) {
        try {
            NetworkInfo v0_1 = ((ConnectivityManager) arg2.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (v0_1 != null && (v0_1.isAvailable())) {
                int v0_2 = v0_1.getType();
                return v0_2;
            }
        } catch (Throwable v0) {
            return -2;
        }

        return -1;
    }

    static String e(Context arg3) {
        String v0_1;
        try {
            v0_1 = ((WifiManager) arg3.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getBSSID();
        } catch (Throwable v0) {
            Logger.d("failed to get BSSID: " + v0);
            v0_1 = null;
        }

        return v0_1;
    }

    public final void a(Context arg9, String arg10) {
        Class v2;
        LinkedList v4;
        int v0_2;
        if (!this.k.get() && this.e != null && this.e.size() > 0 && arg9 != null) {
            try {
                if (!ProcessUtils.b(arg9)) {
                    return;
                }

                if (!AppConfig.getInstance(arg9).m()) {
                    return;
                }

                if (Logger.debug()) {
                    Logger.d("NetChannelSelect", "trySelect");
                }

                if (!android.support.design.a.isNetworkAvailable(arg9)) {
                    return;
                }

                AppConfig v0_1 = AppConfig.getInstance(arg9);
                if (v0_1.e == null) {
                    v0_2 = 0;
                } else if (v0_1.e.a(v0_1.f)) {
                    v0_2 = 1;
                } else {
                    v0_2 = 0;
                }

//                label_31:
                if (v0_2 == 0) {
                    return;
                }

                v4 = new LinkedList();
                v2 = NetChannelSelect.class;
                synchronized (v2) {
                    Iterator v3 = this.e.entrySet().iterator();
                    while (v3.hasNext()) {
                        Object value = ((Map.Entry) v3.next()).getValue();
                        ((List) v4).add(value);
                    }
                }

                try {
                    this.k.getAndSet(true);
                    this.d = System.currentTimeMillis();
                    this.n.removeMessages(105);
                    this.n.sendEmptyMessageDelayed(105, ((long) (this.a * 1000)));
                    this.j = d(arg9);
                    if (this.j == 1) {
                        this.i = e(arg9);
                    }

                    if (this.h.get() >= 9223372036854775807L) {
                        this.h.getAndSet(-1);
                    }

                    this.h.getAndIncrement();
                    new h(this, "SelectThread", Priority.NORMAL, ((List) v4), arg9, arg10).start();
                    return;
                } catch (Throwable v0) {
                    v0.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public final Pair a() {
        Pair v0_1;
        Class v1 = NetChannelSelect.class;
        synchronized (v1) {
            if (this.f != null) {
                if (Logger.debug()) {
                    Logger.d("NetChannelSelect", "getSelectPair mSelectedNetChannel = " + this.f.toString());
                }

                v0_1 = new Pair(this.f.j, this.f.a);
            } else {
                v0_1 = null;
            }

            return v0_1;
        }
    }

    public final void a(Context arg11, JSONObject arg12) {
        Class v1_1;
        int v1;
        LinkedHashMap v4;
        JSONArray v2;
        if (arg11 != null && arg12 != null) {
            try {
                v2 = arg12.optJSONArray("i_host_list");
                if (Logger.debug()) {
                    Logger.d("NetChannelSelect", "fromJson = " + v2.toString());
                }

                if (arg12.has("i_host_last_select_time")) {
                    this.d = arg12.optLong("i_host_last_select_time");
                }

                this.a = arg12.optInt("i_host_select_interval", 1800);
                this.b = arg12.optInt("i_host_select_interval_http_timeout", 60);
                this.c = arg12.optInt("i_host_max_fail", 2);
                int v3 = v2.length();
                v4 = new LinkedHashMap();
                v1 = 0;
                while (true) {
                    if (v1 >= v3) {
                        label_95:
                        v1_1 = NetChannelSelect.class;
                        synchronized (v1_1) {
                            this.e.clear();
                            this.e.putAll(((Map) v4));
                            if (this.f != null && !this.e.containsKey(this.f.a)) {
                                if (Logger.debug()) {
                                    Logger.d("NetChannelSelect", "select from update");
                                }

                                this.a(arg11, "onUpdate");
                            }
                            return;
                        }
                    }

                    try {
                        g$a v5 = new g$a(this);
                        JSONObject v0_2 = v2.optJSONObject(v1);
                        if (v0_2 != null) {
                            v5.a = v0_2.optString("host");
                            v5.b = v0_2.optInt("max_time");
                            v5.c = v0_2.optInt("weight_time");
                            v5.d = v0_2.optInt("https_select_cost", -1);
                            v5.e = v0_2.optLong("https_select_time", -1);
                            v5.f = v0_2.optInt("https_status", -1);
                            v5.g = v0_2.optInt("http_select_cost", -1);
                            v5.h = v0_2.optLong("http_select_time", -1);
                            v5.i = v0_2.optInt("http_status", -1);
                            v5.j = v0_2.optString("scheme");
                        }

                        Object v0_3 = this.e.get(v5.a);
                        if (v0_3 != null) {
                            v5.a(((g$a) v0_3));
                        }

                        ((Map) v4).put(v5.a, v5);
                        ++v1;
                        continue;
                    } catch (Exception v0_1) {
                        v0_1.printStackTrace();
                    }
                }
            } catch (Throwable v0) {
                v0.printStackTrace();
                return;
            }


        }
    }

    public final void a(SharedPreferences.Editor arg5) {
        if (arg5 != null) {
            try {
                JSONObject v0_1 = this.b();
                if (Logger.debug()) {
                    Logger.d("NetChannelSelect", "saveToSp = " + v0_1.toString());
                }

                if (v0_1 == null) {
                    return;
                }

                arg5.putString("i_host_select", v0_1.toString());
                if (this.f != null) {
                    arg5.putString("i_host_select_netchannel_host", this.f.a);
                } else {
                    arg5.putString("i_host_select_netchannel_host", "");
                }

                arg5.putInt("i_host_select_interval", this.a);
                arg5.putInt("i_host_select_interval_http_timeout", this.b);
                arg5.putInt("i_host_max_fail", this.c);
                arg5.putLong("i_host_last_select_time", this.d);
                arg5.putLong("i_host_atomic_long", this.h.longValue());
                arg5.putString("i_host_last_bssid", this.i);
                arg5.putInt("i_host_last_net_type", this.j);
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
    }

    final void a(g$a arg3) {
        g$a v0;
        this.f = arg3;
        if (arg3 == null) {
            v0 = null;
        } else {
            v0 = new g$a(this);
            v0.a = arg3.a;
            v0.b = arg3.b;
            v0.c = arg3.c;
            v0.a(arg3);
        }

        this.mUiSelectedNetChannel = v0;
    }

    public final void b(Context arg5) {
        if (arg5 != null) {
            try {
                if (System.currentTimeMillis() - this.d <= (((long) (this.a * 1000)))) {
                    return;
                }

                if (Logger.debug()) {
                    Logger.d("NetChannelSelect", "select from onActivityResume");
                }

                this.a(arg5, "onActivityResume");
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
    }

    private JSONObject b() {
        if (this.e != null && this.e.size() > 0) {
            JSONArray v1 = new JSONArray();
            Class v2 = NetChannelSelect.class;
            synchronized (v2) {
                Iterator v3 = this.e.entrySet().iterator();
                while (true) {
                    if (v3.hasNext()) {
                        Object v0_1 = v3.next();
                        if (v0_1 != null) {
                            try {
                                g$a aVar = (g$a) ((Map.Entry) v0_1).getValue();
                                v1.put(aVar.c());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            continue;
                        } else {
                            continue;
                        }
                    } else {
                        break;
                    }
                }
            }

            JSONObject v0_3 = new JSONObject();
            try {
                v0_3.put("i_host_list", v1);
            } catch (Exception v1_1) {
                v1_1.printStackTrace();
            }
            return v0_3;
        }

        return null;
    }

    public final void c(Context arg4) {
        Class v1;
        if (arg4 != null) {
            v1 = AppConfig.class;
            synchronized (v1) {
                SharedPreferences.Editor v0_1 = arg4.getSharedPreferences("ss_app_config", 0).edit();
                this.a(v0_1);
                SharedPreferenceCompat.commit(v0_1);
                return;
            }
        }
    }

    public void handleMsg(Message message) {
        if (message != null) {
            try {
                switch (message.what) {
                    case 105: {
                        if (Logger.debug()) {
                            Logger.d("NetChannelSelect", "get MSG_NET_CHANNEL_TIME_OUT");
                        }

                        this.b(this.m);
                        this.n.sendEmptyMessageDelayed(105, ((long) (this.a * 1000)));
                    }
                }
                return;
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
    }

    public static final class g$a {
        public final NetChannelSelect l;
        public String a;
        public String j;
        public int k;
        int b;
        int c;
        int d;
        long e;
        int f;
        int g;
        long h;
        int i;

        g$a(NetChannelSelect arg5) {
            this.l = arg5;
            this.d = -1;
            this.e = -1;
            this.f = -1;
            this.g = -1;
            this.h = -1;
            this.i = -1;
            this.j = "";
            this.k = 0;
        }

        public final void a(g$a arg3) {
            if (arg3 != null) {
                this.d = arg3.d;
                this.e = arg3.e;
                this.f = arg3.f;
                this.g = arg3.g;
                this.h = arg3.h;
                this.i = arg3.i;
                this.j = arg3.j;
            }
        }

        public final boolean a() {
            boolean v0 = true;
            int v2 = -1;
            if (this.d != v2) {
                if (this.d > this.b) {
                    return false;
                }
            } else if (this.g == v2 || this.g > this.b) {
                label_13:
                v0 = false;
            }

            return v0;
        }

        public final int b() {
            int v0;
            int v1 = -1;
            if (this.d != v1) {
                v0 = this.d + this.c;
            } else if (this.g != v1) {
                v0 = this.g + this.c;
            } else {
                v0 = 2147483647;
            }

            return v0;
        }

        public final JSONObject c() throws JSONException {
            JSONObject v0 = new JSONObject();
            v0.put("host", this.a);
            v0.put("max_time", this.b);
            v0.put("weight_time", this.c);
            v0.put("https_select_cost", this.d);
            v0.put("https_select_time", this.e);
            v0.put("https_status", this.f);
            v0.put("http_select_cost", this.g);
            v0.put("http_select_time", this.h);
            v0.put("http_status", this.i);
            v0.put("scheme", this.j);
            return v0;
        }

        public final String toString() {
            return "NetChannel{host=\'" + this.a + '\''
                    + ", maxTime=" + this.b
                    + ", weightTime=" + this.c
                    + ", httpsSelectCost=" + this.d
                    + ", httpsSelectTime=" + this.e
                    + ", httpsStatus=" + this.f
                    + ", httpSelectCost=" + this.g
                    + ", httpSelectTime=" + this.h
                    + ", httpStatus=" + this.i
                    + ", scheme=\'" + this.j + '\'' + '}';
        }
    }
}

